package com.cheyun.netsalev3.viewmodel

import android.content.Intent
import android.view.View
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import com.cheyun.netsalev3.bean.*
import com.cheyun.netsalev3.repository.HomeTaskFragment1Repository
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.view.CuesAllocationActivity
import com.cheyun.netsalev3.view.H5Activity
import com.cheyun.netsalev3.view.MessageListActivity
import com.cheyun.netsalev3.view.defeataudit.DefeatAuditActivity
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList


class HomeTaskFragment1ViewModel(var homeTaskFragment1Repository: HomeTaskFragment1Repository) : ViewModel() {
    /********消息相关*******/
    var showMessage = ObservableField<String>("")
    var showMum = ObservableField<Int>(0)
    var isShow = ObservableField<Boolean>(false)
    var isHuafeiTipShow = ObservableField<Boolean>(false)
    var isHuafeiTipShow2 = MutableLiveData<Boolean>()
    var huafeiMsg = ObservableField<String>("")
    var firstMessageType = "";
    /********消息相关*******/

    var names = arrayListOf("今日邀约", "今日到店", "今日订车", "今日提车",
            "等待跟进", "已经跟进", "跟进逾期", "虚假战败")
    //头部数据
    var listTop = MutableLiveData<List<HomeTopParam>>()
    //内容数据
    var listContent = MutableLiveData<List<MyData>>()

    //用于显示的日期
    var showTime = ObservableField<String>()
    //是否日期到第一个
    var isForward = ObservableField<Boolean>(false)
    //是否日期到最后
    var isBack = ObservableField<Boolean>(false)
    //当前时间位置
    var timePosition = 10
    //时间数据
    var timelist = ArrayList<CalendarParam>()
    //请求数据
    var manageParam = ObservableField<ManageParam>()
    var manageParamss = MutableLiveData<ManageParam>()
    //头部数据
    var temlistTop = ArrayList<HomeTopParam>()
    //头部item选中位置
    var topPosition = 0
    //头部显示名称
    var topName = ObservableField<String>("今日邀约")

    init {
        var dateFormat = SimpleDateFormat("yyyy-MM-dd")
        for (i in -10..10) {
            val c = Calendar.getInstance()
            c.add(Calendar.DAY_OF_MONTH, i)
            var date = dateFormat.format(c.timeInMillis)
            var item = CalendarParam(date, c)
            timelist.add(item)
            //今天
            if (i == 0) {
                timePosition = 10
                showTime.set(item.showTime)
            }

        }
        temlistTop.clear()
        for (i in names.indices) {
            var homeTopParam = HomeTopParam(names[i], "0")
            homeTopParam.IsSelect = i == 0
            temlistTop.add(homeTopParam)
        }
        listTop.postValue(temlistTop)
        homeTaskFragment1Repository.getManagelist(timelist[timePosition].time, this::setData)
    }

    var networkState = Transformations.switchMap(manageParamss) {
        homeTaskFragment1Repository.networkState
    }!!

    /**
     * 消息回调
     */
    fun message(item: NoticeData) {
        var lists = item.rows
        if (lists.isNotEmpty()) {
            if(item.unread>0) {
                showMessage.set(lists[0].message)
                firstMessageType = lists[0].ckey;
            }else{
                showMessage.set("暂无新消息")
            }
            isShow.set(true)
        } else {
            showMessage.set("")
            isShow.set(false)
        }
        showMum.set(item.unread)
    }

    /**
     * 数据回调
     */
    fun setData(p: ManageParam) {
        manageParam.set(p)
        manageParamss.postValue(p)
        for (i in names.indices) {
            temlistTop[i].nume = getData(names[i], p.combined)
        }
        listTop.postValue(temlistTop)
        for (i in p.data) {
            i.setTopPosition(topPosition)
        }
        listContent.postValue(p.data)
    }

    /**
     * 筛选数据
     */
    private fun getData(s: String, combined: MyData): String {
        when (s) {
            "今日邀约" -> return combined.invite.toString()
            "今日到店" -> return combined.arrive.toString()
            "今日订车" -> return combined.order.toString()
            "今日提车" -> return combined.deliver.toString()
            "等待跟进" -> return combined.follow_doing.toString()
            "已经跟进" -> return combined.followed.toString()
            "跟进逾期" -> return combined.follow_out.toString()
            "虚假战败" -> return combined.failure.toString()
        }
        return ""
    }

    /**
     * 消息列表
     */
    fun onClickMessage(view: View) {
        view.context.startActivity(Intent(view.context, MessageListActivity::class.java))
    }

    /**
     * 待审核
     */
    fun onClickAudited(view: View) {
        view.context.startActivity(Intent(view.context, DefeatAuditActivity::class.java))
    }

    /**
     * 待分配
     */
    fun onClickFenPei(view: View) {
        view.context.startActivity(Intent(view.context, CuesAllocationActivity::class.java))
    }

    fun onCloseHuafeiTip(view:View){
        isHuafeiTipShow.set(false)
        isHuafeiTipShow2.postValue(false)
    }

    /**
     * 日期向前
     */
    fun onClickForward() {
        timePosition--
        if (timePosition <= 0) {
            timePosition = 0
            isForward.set(true)
        } else {
            isForward.set(false)
        }
        if (timePosition == timelist.size - 1) {
            isBack.set(true)
        } else {
            isBack.set(false)
        }

        var item = timelist[timePosition]
        showTime.set(item.showTime)
        homeTaskFragment1Repository.getManagelist(timelist[timePosition].time, this::setData,false)
    }

    /**
     * 日期向后
     */
    fun onClickBack() {
        timePosition++
        if (timePosition >= timelist.size - 1) {
            timePosition = timelist.size - 1
            isBack.set(true)
        } else {
            isBack.set(false)

        }
        if (timePosition == 0) {
            isForward.set(true)
        } else {
            isForward.set(false)
        }

        var item = timelist[timePosition]
        showTime.set(item.showTime)
        homeTaskFragment1Repository.getManagelist(timelist[timePosition].time, this::setData,false)

    }

    /**
     * 头部item点击
     */
    fun onClickTopItem(view: View, homeTopParam: HomeTopParam, position: Int) {
        topPosition = position
        topName.set(names[position])
        manageParam.get()?.data?.apply {
            for (i in this) {
                i.setTopPosition(topPosition)
            }
            listContent.postValue(this)
        }
        for (item in temlistTop) {
            item.IsSelect = false
        }
        homeTopParam.IsSelect = true

    }

    fun refresh() {
        homeTaskFragment1Repository.getManagelist(timelist[timePosition].time, this::setData)
        homeTaskFragment1Repository.GetMessage(this::message)
        homeTaskFragment1Repository.getMoney (this::moneyCall)
    }

    fun refreshMessage() {
        homeTaskFragment1Repository.GetMessage(this::message)
    }
    fun getMoney() {

//        homeTaskFragment1Repository.getMoney (this::moneyCall)
    }

    fun moneyCall(data: MoneyParam) {
        if(data!=null){
            if(!data.alert.isNullOrEmpty()){
                isHuafeiTipShow.set(true)
                isHuafeiTipShow2.postValue(true)
                huafeiMsg.set(data.alert)
            }
        }
    }
    fun clickRecordTable(view: View) {
        var intent = Intent(view.context, H5Activity::class.java)
        intent.putExtra("url", MySharedPreferences.getLoginData()?.recordTable)
        view.context.startActivity(intent)
    }

}
